package com.anjie.powerproject.util;

import com.anjie.powerproject.entity.Device;
import com.anjie.powerproject.entity.GraphDev;
import org.dom4j.*;

import java.util.ArrayList;
import java.util.List;

public class ReadXml {

    public static List readStringXml(String xml) {
        Document doc = null;
        List<GraphDev> graphDevs = new ArrayList<>();
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element mxGraphModel = doc.getRootElement();
            Element root = mxGraphModel.element("root");
            List<Element> childElements = root.elements();
            for (Element child : childElements) {
                //未知属性名情况下
            /*List<Attribute> attributeList = child.attributes();
            for (Attribute attr : attributeList) {
                System.out.println(attr.getName() + ": " + attr.getValue());
            }*/
                Attribute value = child.attribute("vertex");
                //确定是设备
                if (value!=null&&!"".equals(value.getValue())){
                    Device device = new Device();
                    GraphDev graphDev = new GraphDev();
                    String id = child.attribute("id").getValue();
                    Element mxGeometry = child.element("mxGeometry");
                    if (mxGeometry.attribute("x")!=null) {
                        String x = mxGeometry.attribute("x").getValue();
                        graphDev.setxCoordinates(Float.valueOf(x));
                    }else {
                        graphDev.setxCoordinates(0);
                    }
                    graphDev.setDeviceId(Integer.parseInt(id));
                    if (mxGeometry.attribute("y")!=null) {
                        String y = mxGeometry.attribute("y").getValue();
                        graphDev.setyCoordinates(Float.valueOf(y));
                    }else {
                        graphDev.setyCoordinates(0);
                    }
                    graphDevs.add(graphDev);
                    //System.out.println("解析xml串：id : " + id+"  x : "+graphDev.getxCoordinates()+"  y : "+graphDev.getyCoordinates());
                }
            }
        }catch(DocumentException e){
                e.printStackTrace();
        }

        return graphDevs;

    }

    public static void main(String[] args) {
        String xml = "<mxGraphModel><root><mxCell id=\"0\"/><mxCell id=\"1\" parent=\"0\"/><mxCell id=\"3\" value=\"地源热泵001\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"20\" y=\"150\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"6\" value=\"地埋管001\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"20\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"20\" value=\"水蓄能001\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"20\" y=\"300\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"36\" value=\"辐射地板001\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"300\" y=\"200\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"93\" value=\"生活热水箱001\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"300\" y=\"50\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"95\" value=\"太阳能001\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"20\" y=\"400\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"106\" value=\"(吉林国网测试设备)\" vertex=\"1\" parent=\"1\"><mxGeometry x=\"300\" y=\"400\" width=\"80\" height=\"30\" as=\"geometry\"/></mxCell><mxCell id=\"107\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"3\" target=\"20\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell><mxCell id=\"108\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"3\" target=\"36\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell><mxCell id=\"109\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"3\" target=\"93\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell><mxCell id=\"110\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"6\" target=\"3\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell><mxCell id=\"111\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"20\" target=\"36\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell><mxCell id=\"112\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"95\" target=\"36\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell><mxCell id=\"113\" value=\"传递\" edge=\"1\" parent=\"1\" source=\"106\" target=\"36\"><mxGeometry relative=\"1\" as=\"geometry\"/></mxCell></root></mxGraphModel>\n";
        readStringXml(xml);
    }
}